package com.hdmall.business.common.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hdmall.business.common.vo.AddressResultVo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 工具类
 */
public class JsonUtils {

    /**
     * @Description // 将json串转换成List
     * @Date 2018/11/2 9:54
     */
    public static <T> List<T> jsonToList(String jsonStr, Class<T> tClass, Map map) {
        JSONArray jsonArray = JSONArray.fromObject(jsonStr);

        List<T> tList = new ArrayList<T>();
        for (int i = 0; i < jsonArray.size(); i++) {
            T t = (T) JSONObject.toBean((JSONObject) jsonArray.get(i), tClass, map);
            tList.add(t);
        }
        return tList;
    }

    public static <T> List<T> jsonToList(String jsonStr, Class<T> tClass) {
        JSONArray jsonArray = JSONArray.fromObject(jsonStr);

        List<T> tList = new ArrayList<T>();
        for (int i = 0; i < jsonArray.size(); i++) {
            T t = (T) JSONObject.toBean((JSONObject) jsonArray.get(i), tClass);
            tList.add(t);
        }
        return tList;
    }

    /**
     * 将 Array,list,set 解析成 Json 串
     * @return Json 串
     */
    public static String arrayToJsonStr(Object objs){
        JSONArray json = JSONArray.fromObject(objs);
        return json.toString();
    }

    /***
     * 将javabean对象和map对象 解析成 Json 串
     * @param obj
     * @return
     */
    public static String objectToJsonStr(Object obj){
        JSONObject json = JSONObject.fromObject(obj);
        return json.toString();

    }

    /***
     * 将javabean对象或者map对象 解析成 Json 串,使用JsonConfig 过滤属性
     * @param obj
     * @param config
     * @return
     */
    public static String objectToJsonStr(Object obj, JsonConfig config ){
//      JsonConfig config = new JsonConfig();
//      config.setExcludes(new String[] { "name" });
        JSONObject json = JSONObject.fromObject(obj,config);
        return json.toString();

    }

    /**
     * 将  Json 串 解析成 Array,list,set
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> Collection<T> jsonStrToArray(String jsonStr){
        JSONArray jsonArray = JSONArray.fromObject(jsonStr);
        Object array = JSONArray.toArray(jsonArray);
        return (Collection<T>) array;
    }

    /**
     * 将  Json 串 解析成 Array
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] jsonStrToArray(String jsonStr, Class<T> clazz){
        JSONArray jsonArray = JSONArray.fromObject(jsonStr);
        return (T[]) JSONArray.toArray(jsonArray,clazz);
    }
    /**
     * 将  Json 串 解析成 Collection
     * @return
     */
    public static <T> Collection<T> jsonStrToCollection(String jsonStr, Class<T> clazz){
        JSONArray jsonArray = JSONArray.fromObject(jsonStr);
        @SuppressWarnings("unchecked")
        Collection<T>  array = JSONArray.toCollection(jsonArray,clazz);
        return array;
    }

    /**
     * 将  Json 串 解析成 list
     * @return
     */
    public static <T> List<T> jsonStrToList(String jsonStr, Class<T> clazz){
        return (List<T>) jsonStrToCollection(jsonStr,clazz);
    }
    /**
     * 将  Json 串 解析成 Map或者javabean
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T jsonStrToObject(String jsonStr, Class<T> clazz){
        JSONObject json = JSONObject.fromObject(jsonStr);
        Object obj = JSONObject.toBean(json, clazz);
        return (T) obj;
    }
    public static void main(String[] args) {
        String ad="{\"address\":\"CN|浙江|杭州|None|CHINANET|0|0\",\"content\":{\"address\":\"浙江省杭州市\",\"address_detail\":{\"city\":\"杭州市\",\"city_code\":179,\"district\":\"\",\"province\":\"浙江省\",\"street\":\"\",\"street_number\":\"\"},\"point\":{\"x\":\"120.21937542\",\"y\":\"30.25924446\"}},\"status\":0}";
        AddressResultVo addressResultVo=jsonStrToObject(ad,AddressResultVo.class);
    }
}
